Integrated Libraries
This category specifies protocols imported from the outside libraries.
WORK IN PROGRESS: This manual is currently being updated and may not contain all the necessary information.
Introduction
The following libraries were implemented:
- eLua bit module - provides basic bit operations
- some functions borrowed from Lua BitOp
- eLua pack module - packing of data into Lua strings and unpacking data from Lua strings
- standard Lua Math library - mathematical functions
- standard Lua Debug library - debugging
- standard Lua String library - functions working with text
- standard Lua Base library functions
Beware, that linked documentation is not always complete and the article tries to link these for cross-reference purposes
Beware, that while the aforementioned libraries are implemented, not all the functions may be enabled. Consult the rest of the article for further details.
eLua Bit Module library
Since Lua doesn't have (yet) built-in capabilities for bit operations, the bit module was added to eLua to fill this gap. It is based on the bitlib library written by Reuben Thomas (slightly adapted to eLua) and provides basic bit operations (like setting and clearing bits) and bitwise operations.
Overview
| Function | Short Description | Note |
|---|---|---|
| bit.bnot | bitwise negation | |
| bit.band | bitwise AND | |
| bit.bor | bitwise OR | |
| bit.bxor | exclusive bitwise OR | |
| bit.lshift | logical bitwise left shift | |
| bit.rshift | logical bitwise right shift | |
| bit.arshift | arithmetic bitwise right shift | |
| bit.bit | generate a bit number | |
| bit.set | set bits in a number | |
| bit.clear | set bits in a number | |
| bit.isset | check if the value in bits is set | |
| bit.isclear | check if the value in bits is clear |
bit.bnot(value)
print(bit.bnot(0))
Bitwise negation, equivalent to ~value in C.
Arguments
-
Argument 1: value
- the bitwise to negate
Return
-
value: (integer)
- the bitwise negated value of the number
Example
print(bit.bnot(0)) --> -1
print(bit.bnot(-1)) --> 0
print(bit.bnot(0xffffffff)) --> 0
bit.band(value1, value2, ...)
print(bit.band(0x12345678, 0xff))
Returns bitwise AND of all of its arguments. Note that more than two arguments are allowed.
Arguments
- Argument 1: value 1
- Argument 2: value 2
Optional
- Argument 3 (optional): value 3
- Argument 4 (optional)...
Return
- value: (integer) - the bitwise AND of all the arguments
Examples
print(bit.band(1, 2, 4, 8)) --> 0
print(bit.band(0x12345678, 0xff)) --> 120
Lua Script Example
bb=bit.band
function rcntr() local r = bb(g(2)+1) sv(2, r) return b(r) end
bit.bor(value1, value2, ...)
print(bit.bor(0x12345678, 0xff))
Returns bitwise OR of all of its arguments. Note that more than two arguments are allowed.
Arguments
- Argument 1: value 1
- Argument 2: value 2
Optional
- Argument 3 (optional): value 3
- Argument 4 (optional)...
Return
- value: (integer) - the bitwise OR of all the arguments
Examples
print(bit.bor(1, 2, 4, 8)) --> 15
print(bit.bor(0x12345678, 0xff)) --> 305420031
Lua Script Example
if nextWR == 1 then
sendFrames(true)
f = b(0xF4) .. rcntr()
i = 1
k = 0
jj = 1
while k == 0 do
B = 0
for j = 0, 7 do
if i > FLen then
k = 1
jj = j
break
end
B = bit.bor(B, bit.lshift(tbi(i), j))
i = i + 1
end
if jj ~= 0 then
f = f .. b(B)
end
end
bit.bxor(value1, value2, ...)
print(bit.bxor(0x12345678, 0xff))
Returns exclusive bitwise OR of all of its arguments. Note that more than two arguments are allowed.
Arguments
- Argument 1: value 1
- Argument 2: value 2
Optional
- Argument 3 (optional): value 3
- Argument 4 (optional)...
Return
- value: (integer) - the exclusive bitwise OR of all the arguments
Examples
print(bit.bxor(1, 2, 4, 8)) --> 15
print(bit.bxor(0x12345678, 0xff)) --> 305419911
Lua Script Example
elseif cmd == 5 then
len = wf("show-length")
idchs = 0
for i=1,len do
_,tmp=pu(wf("show-index", i), "<I")
idchs = bit.bxor(idchs, tmp)
end
bit.lshift(value, shiftPosition)
print(bit.lshift(1, 8))
Returns the bitwise logical left-shift of its first argument by the number of bits given by the second argument. Logical shifts treat the first argument as an unsigned number and shift in 0-bits.
Arguments
- Argument 1: value - the value to be shifted to left
- Argument 2: shiftPosition - the position we want to shift it to
Return
- value: (integer) - the number shifted left
Examples
print(bit.lshift(1, 0)) --> 1
print(bit.lshift(1, 8)) --> 256
print(bit.lshift(1, 40)) --> 256
print(bit.lshift(0x87654321, 12)) --> 1412567040
Lua Script Example
if nextWR == 1 then
sendFrames(true)
f = b(0xF4) .. rcntr()
i = 1
k = 0
jj = 1
while k == 0 do
B = 0
for j = 0, 7 do
if i > FLen then
k = 1
jj = j
break
end
B = bit.bor(B, bit.lshift(tbi(i), j))
i = i + 1
end
if jj ~= 0 then
f = f .. b(B)
end
end
bit.rshift(value, shiftPosition)
print(bit.rshift(256, 8))
Returns the bitwise logical right-shift of its first argument by the number of bits given by the second argument. Logical shifts treat the first argument as an unsigned number and shift in 0-bits.
Arguments
- Argument 1: value - the value to be shifted right
- Argument 2: shiftPosition - the position we want to shift it to
Return
- value: (integer) - the number shifted right
Examples
print(bit.rshift(256, 8)) --> 1
print(bit.rshift(-256, 8)) --> 16777215
print(bit.rshift(0x87654321, 12)) --> 554580
bit.arshift(value, shiftPosition)
print(bit.arshift(-256, 8))
Returns the bitwise arithmetic right-shift of its first argument by the number of bits given by the second argument. Arithmetic right-shift treats the most-significant bit as a sign bit and replicates it.
Arguments
- Argument 1: value - the value to be shifted right
- Argument 2: shiftPosition - the position we want to shift it to
Return
- value: (integer) - the number shifted right
Examples
print(bit.arshift(256, 8)) --> 1
print(bit.arshift(-256, 8)) --> -1
print(bit.arshift(0x87654321, 12)) --> 554580
bit.bit(position)
print(bit.bit(4))
Generate a number with a 1 bit (used for mask generation), Equivalent to 1 < position in C.
Arguments
- Argument: position - position of the bit that will be set to 1
Return
- value: (integer) - a number with only one 1 bit at position (the rest are set to 0)
Examples
print(bit.bit(4)) --> 16
bit.set(value, position1, position2, ...)
print(bit.set(100, 1, 2, 3))
Sets bits in a number.
Arguments
- Argument 1: value - the base number
- Argument 2: position1 - position of the first bit to set (note: indexing starts at 1 in Lua)
Optional
- Argument 3: position2 - position of the second bit to set (note: indexing starts at 1 in Lua)
- Argument 4...
Return
- value: (integer) - the number with the bit(s) set in the given position(s)
Examples
print(bit.set(100, 1)) --> 102
print(bit.set(100, 1, 2, 3)) --> 110
Lua Script Example
function makeHeader(finalFlag, messageNum)
local header = getIMEI()
header = header .. framecounter("h")
framecounter("i")
header = header .. pack.pack('b', plport)
header = header .. getSignal()
if(finalFlag == 1) then
messageNum = bit.set(messageNum, 6)
end
header = header .. pack.pack('>b2', 0x10, messageNum)
return header
end
bit.clear(value, position1, position2, ...)
print(bit.clear(100, 10, 5, 1))
Clears bits in a number.
Arguments
- Argument 1: value - the base number
- Argument 2: position1 - position of the first bit to clear (note: indexing starts at 1 in Lua)
Optional
- Argument 3: position2 - position of the second bit to clear (note: indexing starts at 1 in Lua)
- Argument 4...
Return
- value: (integer) - the number with the bit(s) cleared in the given position(s)
Examples
print(bit.clear(100, 10, 5, 1)) --> 68
bit.isset(value, position)
print(bit.isset(1, 1111))
Tests if a given bit is set - basically opposite of bit.isclear().
Arguments
- Argument 1: value - the value to test
- Argument 2: position - bit position to test (note: indexing starts at 1 in Lua)
Return
- value: (boolean) -
trueif the bit at the given position is 1, otherwisefalse
Examples
print(bit.isset(2, 0)) --> false
print(bit.isset(1, 1111)) --> true
bit.isclear(value, position)
print(bit.isclear(2, 0))
Tests if a given bit is cleared - basically opposite of bit.isset().
Arguments
- Argument 1: value - the value to test
- Argument 2: position - bit position to test (note: indexing starts at 1 in Lua)
Return
- value: (boolean) -
trueif the bit at the given position is 0, otherwisefalse
Examples
print(bit.isclear(2, 0)) --> true
print(bit.isclear(1, 1111)) --> false
bit.tohex(value, absoluteValue)
print(bit.tohex(-1, -8))
Converts its first argument to a hex string. The number of hex digits is given by the absolute value of the optional second argument. Positive numbers between 1 and 8 generate lowercase hex digits. Negative numbers generate uppercase hex digits. Only the least-significant 4*|n| bits are used. The default is to generate 8 lowercase hex digits.
Arguments
- Argument 1: value - the value that is converted to a hex string
- Argument 2 (optional): absoluteValue - number of hex digits: from -8 to 8; positive generates lowercase hex digits; negative generates uppercase hex digits
Return
- value: (hex string)
Examples
print(bit.tohex(1)) --> 00000001
print(bit.tohex(-1)) --> ffffffff
print(bit.tohex(0xffffffff)) --> ffffffff
print(bit.tohex(-1, -8)) --> FFFFFFFF
print(bit.tohex(0x21, 4)) --> 0021
print(bit.tohex(0x87654321, 4)) --> 4321
bit.rol(value1, value2)
print(bit.rol(0x12345678, 12))
Returns the bitwise left rotation of its first argument by the number of bits given by the second argument. Bits shifted out on one side are shifted back in on the other side. Only the lower 5 bits of the rotate count are used (reduces to the range [0..31]).
Arguments
- Argument 1: value - the value that is modified
- Argument 2: shiftCount - the number of bits to shift it to the left side
Return
- value: (integer) - bitwise left rotation
Examples
print(bit.rol(0x12345678, 12)) --> 1164411171
bit.ror(value1, value2)
print(bit.ror(0x12345678, 12))
Returns the bitwise right rotation of its first argument by the number of bits given by the second argument. Bits shifted out on one side are shifted back in on the other side. Only the lower 5 bits of the rotate count are used (reduces to the range [0..31]).
Arguments
- Argument 1: value - the value that is modified
- Argument 2: shiftCount - the number of bits to shift it to the right side
Return
- value: (integer) - bitwise right rotation
Examples
print(bit.ror(0x12345678, 12)) --> 1736516421
Lua Math Library
Library used mainly for pre-determined mathematical functions.
Note, that the converters use only integers for mathematical functions, therefore only some of them are available to use.
Overview
| Function | Short Description | Note |
|---|---|---|
| math.abs | absolute value | |
| math.huge | returns maximum numerical value | |
| math.max | pick the highest value | |
| math.min | pick the lowest value | |
| math.pow | power | |
| math.random | random number | |
| math.random | random number with bottom ceiling | |
| math.randomseed | seed randomization | |
| math.sqrt | square root |
math.abs(integer)
print(math.abs(-5))
The abs() function in Lua's math library returns the absolute value of a given number.
Arguments
- Argument 1: integer
Return
- value: (integer) - the absolute value of the argument
Examples
var value = -5
var absValue = math.abs(value)
print(absValue) --> 5
Lua Script Example
function GetTimestamp()
local sign, zone, Y, m, d, H, M, S, z = "", "", api.getTimeDate(1)
if z then
sign, z = z < 0 and "-" or "+", math.abs(z)
math.huge
print(math.huge)
This creates a maximum possible number, in our case, a maximum value of an integer.
Return
- value: (integer: 2147483647)
Examples
var intMax = math.huge
print(intMax) --> 2147483647
math.max(integer1, integer2, ...)
print(math.max(54, 15, 78))
The max() function picks the highest value from the individual values specified in the arguments.
Arguments
- Argument 1: integer
- Argument 2 (optional): integer
Optional
- Argument 3 (optional): integer
- Argument 4...
Return
- value: (integer) - the highest value of the arguments
Examples
var n1 = 54
var n2 = 15
var n3 = 78
print(math.max(n1, n2, n3)) --> 78
Lua Script Example
function Validate(name) return math.max(var[name].min, math.min(var[name].max, api.getVar(var[name].id) or -1)) end
math.min(integer1, integer2, ...)
print(math.min(54, 15, 78))
The min() function picks the lowest value from the individual values specified in the arguments.
Arguments
- Argument 1: integer
- Argument 2 (optional): integer
Optional
- Argument 3 (optional): integer
- Argument 4...
Return
- value: (integer) - the lowest value of the arguments